package source;

import java.util.*;

public class Player extends Thread {
	
	private final int playerID;

	private int buildDroneCommandCounter = 0;

	private final int maxDrones;
	private final Vector<Drone> playerDrones;
	private Vector<Drone> idleDrones = new Vector<Drone>();

	public Player(int playerID) {
		this.playerID = playerID;
		this.maxDrones = 20;

		SupplyDepot depot = new SupplyDepot();

		playerDrones = new Vector<Drone>(20);

		for (int i = 0; i < 5; i++) {
			Drone drone = new Drone(this.playerID, depot, this);
			playerDrones.add(drone);
			idleDrones.add(drone);
		}
	}

	//metoda de callback in care o anumita drona paseaza jucatorului drona ce tocmai a construit-o
	public void receiveNewDrone(Drone drone) {
		if (drone != null) {
			playerDrones.add(drone);
			drone.start();
			this.addToIdleList(drone);
		}
	}
	
	
	
	//adauga o drona in lista de drone idle
	public void addToIdleList(Drone drone) {
		idleDrones.add(drone);
	}

	//scoate o drona din lista principala
	public void removeDroneFromMainVector(Drone drone) {
		playerDrones.remove(drone);
	}

	@Override
	public void run() {
		
		for (int i = 0; i < 5; i++)
			playerDrones.get(i).start();

		Random random = new Random();
		int command = -1;

		while (true) {
			
			//test de sfarsit de joc
			if (playerDrones.size() == 0) {
				System.out.println("Player_" + playerID + " has left the game");
				break;
			}

			//alegere intre vectorul cu drone idle si vectorul cu toate dronele(inclusiv cele idle)
			//cele idle apar in ambii vectori pt a creste sansele ca o comanda sa fie trimisa lor
			int option = random.nextInt(2);

			Drone drone = null;

			//daca 0 atunci aleg din dronele idle una random
			if (option == 0) {
				if (idleDrones.size() != 0) {
					int droneNumber = random.nextInt(idleDrones.size());
					drone = idleDrones.remove(droneNumber);
				}
			//daca 1 atunci aleg din toate dronele una random
			} else {
				int droneNumber = random.nextInt(playerDrones.size());
				drone = playerDrones.get(droneNumber);
				//daca am nimerit una idle o scot din vectorul de drone idle
				if (drone.isIdle()) {
					idleDrones.remove(drone);
				}
			}

			//daca NU am construit deja nr maxim de drone, generez o comanda random din toate comenzile
			if (buildDroneCommandCounter < maxDrones) {
				command = random.nextInt(18) + 1;
				//daca comanda este pt constructia unei drone, cresc contorul de drone construite
				if (command == 16 || command == 17 || command == 18) {
					buildDroneCommandCounter++;
					System.out.println("Player " + playerID
							+ " has built a drone " + buildDroneCommandCounter);
				}
			//daca am construit nr maxim de drone, generez o comanda random, fara comanda de creare de drona
			} else {
				command = random.nextInt(15) + 1;
			}
			//functie de comanda generata se schimba starea dronei selectate mai sus apeland metoda corespunzatoare
			//Dupa fiecare comanda se apeleaza yield() pt a lua procesorul playerului
			if (drone != null) {
				switch (command) {
				case 1:
					drone.mineGold();
					Player.yield();
					break;
				case 2:
					drone.mineStone();
					Player.yield();
					break;
				case 3:
					drone.cutWood();
					Player.yield();
					break;
				case 4:
					drone.gatherFood();
					Player.yield();
					break;
				//comenzile de construire au sanse mai mari sa fie alese
				case 5:
				case 6:
				case 7:
				case 8:
				case 9:
				case 10:
					drone.buildHouse();
					Player.yield();
					break;
				case 11:
				case 12:
				case 13:
				case 14:
				case 15:
					drone.buildStatue();
					Player.yield();
					break;
				case 16:
				case 17:
				case 18:
					drone.buildDrone();
					Player.yield();
					break;
				}
			}
		}
	}
}
